package network;

public class Request {

	Packet_Chunk [] m_buffered; //the packets expected
	int m_timestamp, m_x, m_y, m_z, m_entity;
	short m_instance, m_count,m_countout;
	public Request(int timestamp, int x, int y, int z, int entity, short instance)
	{
		m_instance=instance;
		m_entity= entity;
		m_x=x;
		m_y=y;
		m_z=z;
		m_timestamp=timestamp;
		m_buffered=new Packet_Chunk[4];	
		m_count=0;
		m_countout=3;
	}
	
	public byte getBitfield()
	{
		byte b=0b1111;
		for (int i=0;i<m_count;i++)
		{
			switch (m_buffered[i].getSegment())
			{
			case 0:
				b-=0b0001;
				break;
			case 1:
				b-=0b0010;
				break;
			case 2:
				b-=0b0100;
				break;
			case 3:
				b-=0b1000;
				break;		
			
			}
		}
		
		return b;
	}
	
	public void setTimestamp(int timestamp)
	{
		m_timestamp=timestamp;
	}
	
	public int getTimestamp()
	{
		return m_timestamp;
	}
	
	public short getInstance()
	{
		return m_instance;
	}
	
	public int getX()
	{
		return m_x;
	}
	public int getY()
	{
		return m_y;
	}
	public int getZ()
	{
		return m_z;
	}
	public int getEntity()
	{
		return m_entity;
	}
	
	public Packet getPacket(int number)
	{
		return m_buffered[number];
	}
	
	public void Clear()
	{
		m_count=0;
		m_buffered=new Packet_Chunk[4];
	
	}
	
	public boolean CanRefresh()
	{
		m_countout--;
		if (m_countout==0)
		{
			return false;
		}
		else
		{
			return true;
		}
	}
	
	public boolean getComplete()
	{
		if (m_count==4)
		{
			byte b=0;
			for (int i=0;i<4;i++)
			{
				b+=0b0001<<m_buffered[i].m_segment;
			}
			if (b!=15)
			{
				m_buffered=new Packet_Chunk[4];
				m_count=0;
				return false;	
			}
			else
			{
			return true;
			}
		}
		else
		{
			return false;
		}	

	}
	
	public void AddPacket(Packet_Chunk p)
	{
		if (m_count<4)
		{
			m_buffered[m_count]=p;
			m_count++;
		}
	}
	
}
